package uloha2;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Riesenie {

    List<Batoh> zoznamInstancii;
    BufferedWriter outStream ;

    public Riesenie(String menoSuboru, BufferedWriter out) {
        zoznamInstancii = new ArrayList<Batoh>();
        nacitajVstupnySuborSInstanciou(menoSuboru,out);
    }

    public void nacitajVstupnySuborSInstanciou(String menoSuboru,BufferedWriter out) {
        File nacitavanySubor;
        FileInputStream fstream = null;
        DataInputStream in = null;
        BufferedReader br = null;

        String nacitaneData;
        String kopiaDat;
        
        double cenaVeci = 0.0;
        double vahaVeci = 0.0;
        int cisloInstance = 0;
        int pocetVeciVBatohu = 0;
        int maxNosnostBatohu = 0;
        try{
            fstream = new FileInputStream(menoSuboru);
            in = new DataInputStream(fstream);
            br = new BufferedReader(new InputStreamReader(in));
            while ((nacitaneData = br.readLine()) != (null) ) {
                Batoh aktualnaInstancia = null;
                Scanner sc = new Scanner(nacitaneData).useDelimiter(" ");
                cisloInstance = sc.nextInt();
                pocetVeciVBatohu = sc.nextInt();
                //System.out.println("Veci je " +pocetVeciVBatohu);
                maxNosnostBatohu = sc.nextInt();
                aktualnaInstancia = new Batoh(maxNosnostBatohu, pocetVeciVBatohu,cisloInstance);
                outStream = out;
                aktualnaInstancia.setStream(out);
                for (int i = 0; i < pocetVeciVBatohu; i++) {
                    vahaVeci = sc.nextDouble();
                    cenaVeci = sc.nextDouble();
                    //System.out.println("Vytvorim objekt, cena je" + cenaVeci + " a vaha veci je " + vahaVeci);
                    VecDoBatohu vkladanaVec = new VecDoBatohu(cenaVeci, vahaVeci, ((double)cenaVeci)/((double)vahaVeci), i+1);
                    aktualnaInstancia.fronta.add(vkladanaVec);
                    aktualnaInstancia.pridajVecDoBatohu(i + 1, vkladanaVec);
                } //vsetky veci su uz priradene do batohu, instancia je nacitana
                zoznamInstancii.add(aktualnaInstancia);
                //System.out.println("Batoh " + aktualnaInstancia.maxHmotnost + " " + aktualnaInstancia.pocetVeciVbatohu + "  bol prave vytvoreny");
            }

        } catch (FileNotFoundException ex) {
            System.out.println("Subor zadany ako argument nebol najdeny");
            Logger.getLogger(Riesenie.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Riesenie.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (Exception ex) {
            System.out.println("Some exception");
        }
        finally {
            try {
                fstream.close();
                in.close();
                br.close();
                
            } catch (IOException ex) {
                Logger.getLogger(Riesenie.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void main(String[] args) {
        BufferedWriter buff = null;
        try {
            long start = System.currentTimeMillis();
            if (args.length != 1) {
                System.out.println(args.length);
                System.out.println("Chybny vstup - nebol zadany vstupny parameter");
                return;
            }
            File file = new File("h");
            if (!file.exists()) {
                file.mkdir();
            }
            File f = new File("h" + File.separator + args[0]);
            try {
                if (!f.exists()) {
                    f.createNewFile();
                }
            } catch (IOException ex) {
                System.out.println("Chyba pri tvorbe vystupneho suboru");
            }
            buff = new BufferedWriter(new FileWriter(f));

            System.out.println("Heuristika");
            Riesenie riesenieInstancii = new Riesenie(args[0], buff);
            Iterator iteruj = riesenieInstancii.zoznamInstancii.iterator();
            while (iteruj.hasNext()) {
                Batoh ruksak = (Batoh) iteruj.next();
                ruksak.vyriesProblemAvypisRiesenie(riesenieInstancii.outStream);
            }
            long stop = System.currentTimeMillis();

            System.out.println("Cas je " + (stop - start) + " milisekund");
        } catch (IOException ex) {
            Logger.getLogger(Riesenie.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                buff.close();
            } catch (IOException ex) {
                Logger.getLogger(Riesenie.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    } // nacitanie vsetkych instancii a ich postupne vyriesenie
}
